package com.yix.service.impl;

import com.yix.entity.User;
import com.yix.mapper.DBMapper;
import com.yix.mapper.UserMapper;
import com.yix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DBMapper dbMapper;

    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public boolean isTableExists(String tableName) {
        return dbMapper.isTableExists(tableName);
    }

    @Override
    public void createTable() {
        String sql = " tp.user (" +
                "            id SERIAL PRIMARY KEY," +
                "            username VARCHAR(50) NOT NULL," +
                "            password VARCHAR(50) NOT NULL," +
                "            address VARCHAR(100)" +
                "        )";
        dbMapper.createTable(sql);
    }

    @Override
    public void dropTable(String tableName) {
        dbMapper.dropTable(tableName);
    }
}